package com.yao;

import java.io.File;

import java.awt.image.BufferedImage;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.filechooser.FileView;

import net.coobird.thumbnailator.Thumbnails;

public class ImageFileView extends FileView {

    @Override
    public String getTypeDescription(File f) {
        String extension = Utils.getExtension(f);
        String type = null;
        if (extension != null) {
            if (Utils.isAllowImage(f)) {
                return extension + " image";
            }
        }
        return type;
    }

    @Override
    public Icon getIcon(File f) {
        String extension = Utils.getExtension(f);
        Icon icon = null;

        if (extension != null) {
            if (Utils.isAllowImage(f)) {
                try {
                    BufferedImage thumb = Thumbnails.of(f)
                        .size(30, 30)
                        .asBufferedImage();
                    icon = new ImageIcon(thumb, "Thumbnails");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
            }
        }
        return icon;
    }
}